Inclass-based programming, thefactory method patternis acreational patternthat uses factory methods to deal with the problem ofcreating objectswithout having to specify the exactclassof the object that will be created. This is done by creating objects by calling a factory method—either specified in an interface and implemented by child classes, or implemented in a base class and optionally overridden by derived classes—rather than by calling aconstructor.
using System;
using System.Linq;
namespace ConsoleDemo
{
public class Program
{
static void Main()
{
Factory[] objFactories = new Factory[2];
objFactories[0] = new concreteFactoryforEmployee();
objFactories[1] = new concreteFactoryforFounder();
Anonymous User
04-Mar-2016In class-based programming, the factory method pattern is a creational pattern that uses factory methods to deal with the problem of creating objects without having to specify the exact class of the object that will be created. This is done by creating objects by calling a factory method—either specified in an interface and implemented by child classes, or implemented in a base class and optionally overridden by derived classes—rather than by calling a constructor.
using System;
using System.Linq;
namespace ConsoleDemo
{
public class Program
{
static void Main()
{
Factory[] objFactories = new Factory[2];
objFactories[0] = new concreteFactoryforEmployee();
objFactories[1] = new concreteFactoryforFounder();
foreach (Factory objFactory in objFactories)
{
DetailsMaster objProduct = objFactory.GetDetails();
objProduct.GetDetails();
}
Console.ReadLine();
}
}
abstract class Factory
{
public abstract DetailsMaster GetDetails(); //Factory Method Declaration
}
classconcreteFactoryforEmployee : Factory
{
publicoverrideDetailsMaster GetDetails() //Factory Method Implementation
{
returnnewEmployee();
}
}
classconcreteFactoryforFounder : Factory
{
publicoverrideDetailsMaster GetDetails() //Factory Method Implementation
{
returnnewFounder();
}
}
interfaceDetailsMaster
{
void GetDetails();
}
classEmployee : DetailsMaster
{
publicvoid GetDetails()
{
Console.WriteLine("Emplyee Details");
Console.WriteLine("======================================");
Console.WriteLine("Name : Rahul");
Console.WriteLine("Join Date : 03/03/206");
Console.WriteLine("designation : Software Developer");
Console.WriteLine("======================================");
}
}
classFounder : DetailsMaster
{
publicvoid GetDetails()
{
Console.WriteLine("Company Details");
Console.WriteLine("======================================");
Console.WriteLine("Name : Jan Singh");
Console.WriteLine("Company Name : ItechWays");
}
}
}